home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / vi_mode.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  28KB  |  1,334 lines

  1. /* vi_mode.c -- A vi emulation mode for Bash.
  2.    Derived from code written by Jeff Sparkes (jsparkes@bnr.ca).  */
  3.  
  4. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  5.  
  6.    This file is part of the GNU Readline Library, a library for
  7.    reading lines of text with interactive input and history editing.
  8.  
  9.    The GNU Readline Library is free software; you can redistribute it
  10.    and/or modify it under the terms of the GNU General Public License
  11.    as published by the Free Software Foundation; either version 1, or
  12.    (at your option) any later version.
  13.  
  14.    The GNU Readline Library is distributed in the hope that it will be
  15.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  16.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23. #define READLINE_LIBRARY
  24.  
  25. /* **************************************************************** */
  26. /*                                    */
  27. /*            VI Emulation Mode                */
  28. /*                                    */
  29. /* **************************************************************** */
  30. #if defined (HAVE_CONFIG_H)
  31. #  include "config.h"
  32. #endif
  33.  
  34. #include "rlconf.h"
  35.  
  36. #if defined (VI_MODE)
  37.  
  38. #include <sys/types.h>
  39.  
  40. #if defined (HAVE_STDLIB_H)
  41. #  include <stdlib.h>
  42. #else
  43. #  include "ansi_stdlib.h"
  44. #endif /* HAVE_STDLIB_H */
  45.  
  46. #if defined (HAVE_UNISTD_H)
  47. #  include <unistd.h>
  48. #endif
  49.  
  50. #include <stdio.h>
  51.  
  52. /* Some standard library routines. */
  53. #include "rldefs.h"
  54. #include "readline.h"
  55. #include "history.h"
  56.  
  57. #ifndef digit_p
  58. #define digit_p(c)  ((c) >= '0' && (c) <= '9')
  59. #endif
  60.  
  61. #ifndef digit_value
  62. #define digit_value(c) ((c) - '0')
  63. #endif
  64.  
  65. #ifndef member
  66. #define member(c, s) ((c) ? (char *)strchr ((s), (c)) != (char *)NULL : 0)
  67. #endif
  68.  
  69. #ifndef isident
  70. #define isident(c) ((pure_alphabetic (c) || digit_p (c) || c == '_'))
  71. #endif
  72.  
  73. #ifndef exchange
  74. #define exchange(x, y) do {int temp = x; x = y; y = temp;} while (0)
  75. #endif
  76.  
  77. #ifndef VI_COMMENT_BEGIN_DEFAULT
  78. #define VI_COMMENT_BEGIN_DEFAULT "#"
  79. #endif
  80.  
  81. #if defined (STATIC_MALLOC)
  82. static char *xmalloc (), *xrealloc ();
  83. #else
  84. extern char *xmalloc (), *xrealloc ();
  85. #endif /* STATIC_MALLOC */
  86.  
  87. /* Variables imported from readline.c */
  88. extern int rl_point, rl_end, rl_mark, rl_done;
  89. extern FILE *rl_instream;
  90. extern int rl_line_buffer_len, rl_explicit_arg, rl_numeric_arg;
  91. extern Keymap _rl_keymap;
  92. extern char *rl_prompt;
  93. extern char *rl_line_buffer;
  94. extern int rl_arg_sign;
  95.  
  96. extern void _rl_dispatch ();
  97.  
  98. extern void rl_extend_line_buffer ();
  99. extern int rl_vi_check ();
  100.  
  101. /* Non-zero means enter insertion mode. */
  102. static int _rl_vi_doing_insert = 0;
  103.  
  104. /* String inserted into the line by rl_vi_comment (). */
  105. char *rl_vi_comment_begin = (char *)NULL;
  106.  
  107. /* *** UNCLEAN *** */
  108. /* Command keys which do movement for xxx_to commands. */
  109. static char *vi_motion = " hl^$0ftFt;,%wbeWBE|";
  110.  
  111. /* Keymap used for vi replace characters.  Created dynamically since
  112.    rarely used. */
  113. static Keymap vi_replace_map = (Keymap)NULL;
  114.  
  115. /* The number of characters inserted in the last replace operation. */
  116. static int vi_replace_count = 0;
  117.  
  118. /* If non-zero, we have text inserted after a c[motion] command that put
  119.    us implicitly into insert mode.  Some people want this text to be
  120.    attached to the command so that it is `redoable' with `.'. */
  121. static int vi_continued_command = 0;
  122.  
  123. static int _rl_vi_last_command = 'i';    /* default `.' puts you in insert mode */
  124. static int _rl_vi_last_repeat = 1;
  125. static int _rl_vi_last_arg_sign = 1;
  126. static int _rl_vi_last_motion = 0;
  127. static int _rl_vi_last_search_char = 0;
  128. static int _rl_vi_last_replacement = 0;
  129.  
  130. static int vi_redoing = 0;
  131.  
  132. /* Text modification commands.  These are the `redoable' commands. */
  133. static char *vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
  134.  
  135. static int rl_digit_loop1 ();
  136.  
  137. void
  138. _rl_vi_reset_last ()
  139. {
  140.   _rl_vi_last_command = 'i';
  141.   _rl_vi_last_repeat = 1;
  142.   _rl_vi_last_arg_sign = 1;
  143.   _rl_vi_last_motion = 0;
  144. }
  145.  
  146. void
  147. _rl_vi_set_last (key, repeat, sign)
  148.      int key, repeat, sign;
  149. {
  150.   _rl_vi_last_command = key;
  151.   _rl_vi_last_repeat = repeat;
  152.   _rl_vi_last_arg_sign = sign;
  153. }
  154.  
  155. /* Is the command C a VI mode text modification command? */
  156. int
  157. rl_vi_textmod_command (c)
  158.      int c;
  159. {
  160.   return (member (c, vi_textmod));
  161. }
  162.  
  163. /* Bound to `.'.  Called from command mode, so we know that we have to
  164.    redo a text modification command.  The default for _rl_vi_last_command
  165.    puts you back into insert mode. */
  166. rl_vi_redo (count, c)
  167.      int count, c;
  168. {
  169.   if (!rl_explicit_arg)
  170.     {
  171.       rl_numeric_arg = _rl_vi_last_repeat;
  172.       rl_arg_sign = _rl_vi_last_arg_sign;
  173.     }
  174.  
  175.   vi_redoing = 1;
  176.   _rl_dispatch (_rl_vi_last_command, _rl_keymap);
  177.   vi_redoing = 0;
  178.  
  179.   return (0);
  180. }
  181.     
  182. /* Yank the nth arg from the previous line into this line at point. */
  183. rl_vi_yank_arg (count, key)
  184.      int count, key;
  185. {
  186.   /* Readline thinks that the first word on a line is the 0th, while vi
  187.      thinks the first word on a line is the 1st.  Compensate. */
  188.   if (rl_explicit_arg)
  189.     rl_yank_nth_arg (count - 1, 0);
  190.   else
  191.     rl_yank_nth_arg ('$', 0);
  192.  
  193.   return (0);
  194. }
  195.  
  196. /* With an argument, move back that many history lines, else move to the
  197.    beginning of history. */
  198. rl_vi_fetch_history (count, c)
  199.      int count, c;
  200. {
  201.   int current = where_history ();
  202.  
  203.   /* Giving an argument of n means we want the nth command in the history
  204.      file.  The command number is interpreted the same way that the bash
  205.      `history' command does it -- that is, giving an argument count of 450
  206.      to this command would get the command listed as number 450 in the
  207.      output of `history'. */
  208.   if (rl_explicit_arg)
  209.     {
  210.       int wanted = history_base + current - count;
  211.       if (wanted <= 0)
  212.         rl_beginning_of_history (0, 0);
  213.       else
  214.         rl_get_previous_history (wanted);
  215.     }
  216.   else
  217.     rl_beginning_of_history (count, 0);
  218.   return (0);
  219. }
  220.  
  221. /* Search again for the last thing searched for. */
  222. rl_vi_search_again (count, key)
  223.      int count, key;
  224. {
  225.   switch (key)
  226.     {
  227.     case 'n':
  228.       rl_noninc_reverse_search_again (count, key);
  229.       break;
  230.  
  231.     case 'N':
  232.       rl_noninc_forward_search_again (count, key);
  233.       break;
  234.     }
  235.   return (0);
  236. }
  237.  
  238. /* Do a vi style search. */
  239. rl_vi_search (count, key)
  240.      int count, key;
  241. {
  242.   switch (key)
  243.     {
  244.     case '?':
  245.       rl_noninc_forward_search (count, key);
  246.       break;
  247.  
  248.     case '/':
  249.       rl_noninc_reverse_search (count, key);
  250.       break;
  251.  
  252.     default:
  253.       ding ();
  254.       break;
  255.     }
  256.   return (0);
  257. }
  258.  
  259. /* Completion, from vi's point of view. */
  260. rl_vi_complete (ignore, key)
  261.      int ignore, key;
  262. {
  263.   if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
  264.     {
  265.       if (!whitespace (rl_line_buffer[rl_point + 1]))
  266.     rl_vi_end_word (1, 'E');
  267.       rl_point++;
  268.     }
  269.  
  270.   if (key == '*')
  271.     rl_complete_internal ('*');    /* Expansion and replacement. */
  272.   else if (key == '=')
  273.     rl_complete_internal ('?');    /* List possible completions. */
  274.   else if (key == '\\')
  275.     rl_complete_internal (TAB);    /* Standard Readline completion. */
  276.   else
  277.     rl_complete (0, key);
  278.  
  279.   if (key == '*' || key == '\\')
  280.     {
  281.       _rl_vi_set_last (key, 1, rl_arg_sign);
  282.       rl_vi_insertion_mode ();
  283.     }
  284.   return (0);
  285. }
  286.  
  287. /* Tilde expansion for vi mode. */
  288. rl_vi_tilde_expand (ignore, key)
  289.      int ignore, key;
  290. {
  291.   rl_tilde_expand (0, key);
  292.   _rl_vi_set_last (key, 1, rl_arg_sign);    /* XXX */
  293.   rl_vi_insertion_mode ();
  294.   return (0);
  295. }
  296.  
  297. /* Previous word in vi mode. */
  298. rl_vi_prev_word (count, key)
  299.      int count, key;
  300. {
  301.   if (count < 0)
  302.     return (rl_vi_next_word (-count, key));
  303.  
  304.   if (rl_point == 0)
  305.     {
  306.       ding ();
  307.       return (0);
  308.     }
  309.  
  310.   if (uppercase_p (key))
  311.     rl_vi_bWord (count);
  312.   else
  313.     rl_vi_bword (count);
  314.  
  315.   return (0);
  316. }
  317.  
  318. /* Next word in vi mode. */
  319. rl_vi_next_word (count, key)
  320.      int count, key;
  321. {
  322.   if (count < 0)
  323.     return (rl_vi_prev_word (-count, key));
  324.  
  325.   if (rl_point >= (rl_end - 1))
  326.     {
  327.       ding ();
  328.       return (0);
  329.     }
  330.  
  331.   if (uppercase_p (key))
  332.     rl_vi_fWord (count);
  333.   else
  334.     rl_vi_fword (count);
  335.   return (0);
  336. }
  337.  
  338. /* Move to the end of the ?next? word. */
  339. rl_vi_end_word (count, key)
  340.      int count, key;
  341. {
  342.   if (count < 0)
  343.     {
  344.       ding ();
  345.       return -1;
  346.     }
  347.  
  348.   if (uppercase_p (key))
  349.     rl_vi_eWord (count);
  350.   else
  351.     rl_vi_eword (count);
  352.   return (0);
  353. }
  354.  
  355. /* Move forward a word the way that 'W' does. */
  356. rl_vi_fWord (count)
  357.      int count;
  358. {
  359.   while (count-- && rl_point < (rl_end - 1))
  360.     {
  361.       /* Skip until whitespace. */
  362.       while (!whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  363.     rl_point++;
  364.  
  365.       /* Now skip whitespace. */
  366.       while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  367.     rl_point++;
  368.     }
  369.   return (0);
  370. }
  371.  
  372. rl_vi_bWord (count)
  373.      int count;
  374. {
  375.   while (count-- && rl_point > 0)
  376.     {
  377.       /* If we are at the start of a word, move back to whitespace so
  378.      we will go back to the start of the previous word. */
  379.       if (!whitespace (rl_line_buffer[rl_point]) &&
  380.       whitespace (rl_line_buffer[rl_point - 1]))
  381.     rl_point--;
  382.  
  383.       while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
  384.     rl_point--;
  385.  
  386.       if (rl_point > 0)
  387.     {
  388.       while (--rl_point >= 0 && !whitespace (rl_line_buffer[rl_point]));
  389.       rl_point++;
  390.     }
  391.     }
  392.   return (0);
  393. }
  394.  
  395. rl_vi_eWord (count)
  396.      int count;
  397. {
  398.   while (count-- && rl_point < (rl_end - 1))
  399.     {
  400.       if (!whitespace (rl_line_buffer[rl_point]))
  401.     rl_point++;
  402.  
  403.       /* Move to the next non-whitespace character (to the start of the
  404.      next word). */
  405.       while (++rl_point < rl_end && whitespace (rl_line_buffer[rl_point]));
  406.  
  407.       if (rl_point && rl_point < rl_end)
  408.     {
  409.       /* Skip whitespace. */
  410.       while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  411.         rl_point++;
  412.  
  413.       /* Skip until whitespace. */
  414.       while (rl_point < rl_end && !whitespace (rl_line_buffer[rl_point]))
  415.         rl_point++;
  416.  
  417.       /* Move back to the last character of the word. */
  418.       rl_point--;
  419.     }
  420.     }
  421.   return (0);
  422. }
  423.  
  424. rl_vi_fword (count)
  425.      int count;
  426. {
  427.   while (count-- && rl_point < (rl_end - 1))
  428.     {
  429.       /* Move to white space (really non-identifer). */
  430.       if (isident (rl_line_buffer[rl_point]))
  431.     {
  432.       while (isident (rl_line_buffer[rl_point]) && rl_point < rl_end)
  433.         rl_point++;
  434.     }
  435.       else /* if (!whitespace (rl_line_buffer[rl_point])) */
  436.     {
  437.       while (!isident (rl_line_buffer[rl_point]) &&
  438.          !whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  439.         rl_point++;
  440.     }
  441.  
  442.       /* Move past whitespace. */
  443.       while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  444.     rl_point++;
  445.     }
  446.   return (0);
  447. }
  448.  
  449. rl_vi_bword (count)
  450.      int count;
  451. {
  452.   while (count-- && rl_point > 0)
  453.     {
  454.       int last_is_ident;
  455.  
  456.       /* If we are at the start of a word, move back to whitespace
  457.      so we will go back to the start of the previous word. */
  458.       if (!whitespace (rl_line_buffer[rl_point]) &&
  459.       whitespace (rl_line_buffer[rl_point - 1]))
  460.     rl_point--;
  461.  
  462.       /* If this character and the previous character are `opposite', move
  463.      back so we don't get messed up by the rl_point++ down there in
  464.      the while loop.  Without this code, words like `l;' screw up the
  465.      function. */
  466.       last_is_ident = isident (rl_line_buffer[rl_point - 1]);
  467.       if ((isident (rl_line_buffer[rl_point]) && !last_is_ident) ||
  468.       (!isident (rl_line_buffer[rl_point]) && last_is_ident))
  469.     rl_point--;
  470.  
  471.       while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
  472.     rl_point--;
  473.  
  474.       if (rl_point > 0)
  475.     {
  476.       if (isident (rl_line_buffer[rl_point]))
  477.         while (--rl_point >= 0 && isident (rl_line_buffer[rl_point]));
  478.       else
  479.         while (--rl_point >= 0 && !isident (rl_line_buffer[rl_point]) &&
  480.            !whitespace (rl_line_buffer[rl_point]));
  481.       rl_point++;
  482.     }
  483.     }
  484.   return (0);
  485. }
  486.  
  487. rl_vi_eword (count)
  488.      int count;
  489. {
  490.   while (count-- && rl_point < rl_end - 1)
  491.     {
  492.       if (!whitespace (rl_line_buffer[rl_point]))
  493.     rl_point++;
  494.  
  495.       while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  496.     rl_point++;
  497.  
  498.       if (rl_point < rl_end)
  499.     {
  500.       if (isident (rl_line_buffer[rl_point]))
  501.         while (++rl_point < rl_end && isident (rl_line_buffer[rl_point]));
  502.       else
  503.         while (++rl_point < rl_end && !isident (rl_line_buffer[rl_point])
  504.            && !whitespace (rl_line_buffer[rl_point]));
  505.     }
  506.       rl_point--;
  507.     }
  508.   return (0);
  509. }
  510.  
  511. rl_vi_insert_beg (count, key)
  512.      int count, key;
  513. {
  514.   rl_beg_of_line ();
  515.   rl_vi_insertion_mode ();
  516.   return (0);
  517. }
  518.  
  519. rl_vi_append_mode (count, key)
  520.      int count, key;
  521. {
  522.   if (rl_point < rl_end)
  523.     rl_point++;
  524.   rl_vi_insertion_mode ();
  525.   return (0);
  526. }
  527.  
  528. rl_vi_append_eol (count, key)
  529.      int count, key;
  530. {
  531.   rl_end_of_line ();
  532.   rl_vi_append_mode ();
  533.   return (0);
  534. }
  535.  
  536. /* What to do in the case of C-d. */
  537. rl_vi_eof_maybe (count, c)
  538.      int count, c;
  539. {
  540.   return (rl_newline (1, '\n'));
  541. }
  542.  
  543. /* Insertion mode stuff. */
  544.  
  545. /* Switching from one mode to the other really just involves
  546.    switching keymaps. */
  547. rl_vi_insertion_mode (count, key)
  548.      int count, key;
  549. {
  550.   _rl_keymap = vi_insertion_keymap;
  551.   return (0);
  552. }
  553.  
  554. void
  555. _rl_vi_done_inserting ()
  556. {
  557.   if (_rl_vi_doing_insert)
  558.     {
  559.       rl_end_undo_group ();
  560.       /* Now, the text between rl_undo_list->next->start and
  561.      rl_undo_list->next->end is what was inserted while in insert
  562.      mode. */
  563.       _rl_vi_doing_insert = 0;
  564.       vi_continued_command = 1;
  565.     }
  566.   else
  567.     vi_continued_command = 0;
  568. }
  569.  
  570. rl_vi_movement_mode (count, key)
  571.      int count, key;
  572. {
  573.   if (rl_point > 0)
  574.     rl_backward (1);
  575.  
  576. #if 0
  577.   _rl_vi_reset_last ();
  578. #endif
  579.  
  580.   _rl_keymap = vi_movement_keymap;
  581.   _rl_vi_done_inserting ();
  582.   return (0);
  583. }
  584.  
  585. rl_vi_arg_digit (count, c)
  586.      int count, c;
  587. {
  588.   if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
  589.     return (rl_beg_of_line ());
  590.   else
  591.     return (rl_digit_argument (count, c));
  592. }
  593.  
  594. rl_vi_change_case (count, ignore)
  595.      int count, ignore;
  596. {
  597.   char c = 0;
  598.  
  599.   /* Don't try this on an empty line. */
  600.   if (rl_point >= rl_end)
  601.     return (0);
  602.  
  603.   while (count-- && rl_point < rl_end)
  604.     {
  605.       if (uppercase_p (rl_line_buffer[rl_point]))
  606.     c = to_lower (rl_line_buffer[rl_point]);
  607.       else if (lowercase_p (rl_line_buffer[rl_point]))
  608.     c = to_upper (rl_line_buffer[rl_point]);
  609.       else
  610.     {
  611.       /* Just skip over characters neither upper nor lower case. */
  612.       rl_forward (1);
  613.       continue;
  614.     }
  615.  
  616.       /* Vi is kind of strange here. */
  617.       if (c)
  618.     {
  619.       rl_begin_undo_group ();
  620.       rl_delete (1, c);
  621.       rl_insert (1, c);
  622.       rl_end_undo_group ();
  623.       rl_vi_check ();
  624.         }
  625.       else
  626.     rl_forward (1);
  627.     }
  628.   return (0);
  629. }
  630.  
  631. rl_vi_put (count, key)
  632.      int count, key;
  633. {
  634.   if (!uppercase_p (key) && (rl_point + 1 <= rl_end))
  635.     rl_point++;
  636.  
  637.   rl_yank ();
  638.   rl_backward (1);
  639.   return (0);
  640. }
  641.  
  642. rl_vi_check ()
  643. {
  644.   if (rl_point && rl_point == rl_end)
  645.     rl_point--;
  646.   return (0);
  647. }
  648.  
  649. rl_vi_column (count, key)
  650.      int count, key;
  651. {
  652.   if (count > rl_end)
  653.     rl_end_of_line ();
  654.   else
  655.     rl_point = count - 1;
  656.   return (0);
  657. }
  658.  
  659. int
  660. rl_vi_domove (key, nextkey)
  661.      int key, *nextkey;
  662. {
  663.   int c, save;
  664.   int old_end;
  665.  
  666.   rl_mark = rl_point;
  667.   c = rl_read_key ();
  668.   *nextkey = c;
  669.  
  670.   if (!member (c, vi_motion))
  671.     {
  672.       if (digit_p (c))
  673.     {
  674.       save = rl_numeric_arg;
  675.       rl_numeric_arg = digit_value (c);
  676.       rl_digit_loop1 ();
  677.       rl_numeric_arg *= save;
  678.       c = rl_read_key ();    /* real command */
  679.       *nextkey = c;
  680.     }
  681.       else if (key == c && (key == 'd' || key == 'y' || key == 'c'))
  682.     {
  683.       rl_mark = rl_end;
  684.       rl_beg_of_line ();
  685.       _rl_vi_last_motion = c;
  686.       return (0);
  687.     }
  688.       else
  689.     return (-1);
  690.     }
  691.  
  692.   _rl_vi_last_motion = c;
  693.  
  694.   /* Append a blank character temporarily so that the motion routines
  695.      work right at the end of the line. */
  696.   old_end = rl_end;
  697.   rl_line_buffer[rl_end++] = ' ';
  698.   rl_line_buffer[rl_end] = '\0';
  699.  
  700.   _rl_dispatch (c, _rl_keymap);
  701.  
  702.   /* Remove the blank that we added. */
  703.   rl_end = old_end;
  704.   rl_line_buffer[rl_end] = '\0';
  705.   if (rl_point > rl_end)
  706.     rl_point = rl_end;
  707.  
  708.   /* No change in position means the command failed. */
  709.   if (rl_mark == rl_point)
  710.     return (-1);
  711.  
  712.   /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
  713.      word.  If we are not at the end of the line, and we are on a
  714.      non-whitespace character, move back one (presumably to whitespace). */
  715.   if ((to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
  716.       !whitespace (rl_line_buffer[rl_point]))
  717.     rl_point--;
  718.  
  719.   /* If cw or cW, back up to the end of a word, so the behaviour of ce
  720.      or cE is the actual result.  Brute-force, no subtlety. */
  721.   if (key == 'c' && rl_point >= rl_mark && (to_upper (c) == 'W'))
  722.     {
  723.       /* Don't move farther back than where we started. */
  724.       while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
  725.     rl_point--;
  726.  
  727.       /* Posix.2 says that if cw or cW moves the cursor towards the end of
  728.      the line, the character under the cursor should be deleted. */
  729.       if (rl_point == rl_mark)
  730.         rl_point++;
  731.       else
  732.     {
  733.       /* Move past the end of the word so that the kill doesn't
  734.          remove the last letter of the previous word.  Only do this
  735.          if we are not at the end of the line. */
  736.       if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point]))
  737.         rl_point++;
  738.     }
  739.     }
  740.  
  741.   if (rl_mark < rl_point)
  742.     exchange (rl_point, rl_mark);
  743.  
  744.   return (0);
  745. }
  746.  
  747. /* A simplified loop for vi. Don't dispatch key at end.
  748.    Don't recognize minus sign? */
  749. static int
  750. rl_digit_loop1 ()
  751. {
  752.   int key, c;
  753.  
  754.   while (1)
  755.     {
  756.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg, 0);
  757.       key = c = rl_read_key ();
  758.  
  759.       if (_rl_keymap[c].type == ISFUNC &&
  760.       _rl_keymap[c].function == rl_universal_argument)
  761.     {
  762.       rl_numeric_arg *= 4;
  763.       continue;
  764.     }
  765.  
  766.       c = UNMETA (c);
  767.       if (digit_p (c))
  768.     {
  769.       if (rl_explicit_arg)
  770.         rl_numeric_arg = (rl_numeric_arg * 10) + digit_value (c);
  771.       else
  772.         rl_numeric_arg = digit_value (c);
  773.       rl_explicit_arg = 1;
  774.     }
  775.       else
  776.     {
  777.       rl_clear_message ();
  778.       rl_stuff_char (key);
  779.       break;
  780.     }
  781.     }
  782.   return (0);
  783. }
  784.  
  785. rl_vi_delete_to (count, key)
  786.      int count, key;
  787. {
  788.   int c;
  789.  
  790.   if (uppercase_p (key))
  791.     rl_stuff_char ('$');
  792.   else if (vi_redoing)
  793.     rl_stuff_char (_rl_vi_last_motion);
  794.  
  795.   if (rl_vi_domove (key, &c))
  796.     {
  797.       ding ();
  798.       return -1;
  799.     }
  800.  
  801.   /* These are the motion commands that do not require adjusting the
  802.      mark. */
  803.   if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end))
  804.     rl_mark++;
  805.  
  806.   rl_kill_text (rl_point, rl_mark);
  807.   return (0);
  808. }
  809.  
  810. rl_vi_change_to (count, key)
  811.      int count, key;
  812. {
  813.   int c, start_pos;
  814.  
  815.   if (uppercase_p (key))
  816.     rl_stuff_char ('$');
  817.   else if (vi_redoing)
  818.     rl_stuff_char (_rl_vi_last_motion);
  819.  
  820.   start_pos = rl_point;
  821.  
  822.   if (rl_vi_domove (key, &c))
  823.     {
  824.       ding ();
  825.       return -1;
  826.     }
  827.  
  828.   /* These are the motion commands that do not require adjusting the
  829.      mark.  c[wW] are handled by special-case code in rl_vi_domove(),
  830.      and already leave the mark at the correct location. */
  831.   if ((strchr (" l|hwW^0%bB", c) == 0) && (rl_mark < rl_end))
  832.     rl_mark++;
  833.  
  834.   /* The cursor never moves with c[wW]. */
  835.   if ((to_upper (c) == 'W') && rl_point < start_pos)
  836.     rl_point = start_pos;
  837.  
  838.   rl_kill_text (rl_point, rl_mark);
  839.  
  840.   rl_begin_undo_group ();
  841.   _rl_vi_doing_insert = 1;
  842.   _rl_vi_set_last (key, count, rl_arg_sign);
  843.   rl_vi_insertion_mode ();
  844.  
  845.   return (0);
  846. }
  847.  
  848. rl_vi_yank_to (count, key)
  849.      int count, key;
  850. {
  851.   int c, save = rl_point;
  852.  
  853.   if (uppercase_p (key))
  854.     rl_stuff_char ('$');
  855.  
  856.   if (rl_vi_domove (key, &c))
  857.     {
  858.       ding ();
  859.       return -1;
  860.     }
  861.  
  862.   /* These are the motion commands that do not require adjusting the
  863.      mark. */
  864.   if ((strchr (" l|h^0%bB", c) == 0) && (rl_mark < rl_end))
  865.     rl_mark++;
  866.  
  867.   rl_begin_undo_group ();
  868.   rl_kill_text (rl_point, rl_mark);
  869.   rl_end_undo_group ();
  870.   rl_do_undo ();
  871.   rl_point = save;
  872.  
  873.   return (0);
  874. }
  875.  
  876. rl_vi_delete (count, key)
  877.      int count, key;
  878. {
  879.   int end;
  880.  
  881.   if (rl_end == 0)
  882.     {
  883.       ding ();
  884.       return -1;
  885.     }
  886.  
  887.   end = rl_point + count;
  888.  
  889.   if (end >= rl_end)
  890.     end = rl_end;
  891.  
  892.   rl_kill_text (rl_point, end);
  893.   
  894.   if (rl_point > 0 && rl_point == rl_end)
  895.     rl_backward (1);
  896.   return (0);
  897. }
  898.  
  899. /* Turn the current line into a comment in shell history.
  900.    A K*rn shell style function. */
  901. rl_vi_comment (count, key)
  902.      int count, key;
  903. {
  904.   rl_beg_of_line ();
  905.  
  906.   if (rl_vi_comment_begin != (char *)NULL)
  907.     rl_insert_text (rl_vi_comment_begin);
  908.   else
  909.     rl_insert_text (VI_COMMENT_BEGIN_DEFAULT);    /* Default. */
  910.  
  911.   rl_redisplay ();
  912.   rl_newline (1, '\010');
  913.   return (0);
  914. }
  915.  
  916. rl_vi_first_print (count, key)
  917.      int count, key;
  918. {
  919.   return (rl_back_to_indent ());
  920. }
  921.  
  922. rl_back_to_indent (ignore1, ignore2)
  923.      int ignore1, ignore2;
  924. {
  925.   rl_beg_of_line ();
  926.   while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  927.     rl_point++;
  928.   return (0);
  929. }
  930.  
  931. /* NOTE: it is necessary that opposite directions are inverses */
  932. #define    FTO     1        /* forward to */
  933. #define BTO    -1        /* backward to */
  934. #define FFIND     2        /* forward find */
  935. #define BFIND    -2        /* backward find */
  936.  
  937. rl_vi_char_search (count, key)
  938.      int count, key;
  939. {
  940.   static char target;
  941.   static int orig_dir, dir;
  942.   int pos;
  943.  
  944.   if (key == ';' || key == ',')
  945.     dir = (key == ';' ? orig_dir : -orig_dir);
  946.   else
  947.     {
  948.       if (vi_redoing)
  949.     target = _rl_vi_last_search_char;
  950.       else
  951.     _rl_vi_last_search_char = target = rl_getc (rl_instream);
  952.  
  953.       switch (key)
  954.         {
  955.         case 't':
  956.           orig_dir = dir = FTO;
  957.           break;
  958.  
  959.         case 'T':
  960.           orig_dir = dir = BTO;
  961.           break;
  962.  
  963.         case 'f':
  964.           orig_dir = dir = FFIND;
  965.           break;
  966.  
  967.         case 'F':
  968.           orig_dir = dir = BFIND;
  969.           break;
  970.         }
  971.     }
  972.  
  973.   pos = rl_point;
  974.  
  975.   while (count--)
  976.     {
  977.       if (dir < 0)
  978.     {
  979.       if (pos == 0)
  980.         {
  981.           ding ();
  982.           return -1;
  983.         }
  984.  
  985.       pos--;
  986.       do
  987.         {
  988.           if (rl_line_buffer[pos] == target)
  989.         {
  990.           if (dir == BTO)
  991.             rl_point = pos + 1;
  992.           else
  993.             rl_point = pos;
  994.           break;
  995.         }
  996.         }
  997.       while (pos--);
  998.  
  999.       if (pos < 0)
  1000.         {
  1001.           ding ();
  1002.           return -1;
  1003.         }
  1004.     }
  1005.       else
  1006.     {            /* dir > 0 */
  1007.       if (pos >= rl_end)
  1008.         {
  1009.           ding ();
  1010.           return -1;
  1011.         }
  1012.  
  1013.       pos++;
  1014.       do
  1015.         {
  1016.           if (rl_line_buffer[pos] == target)
  1017.         {
  1018.           if (dir == FTO)
  1019.             rl_point = pos - 1;
  1020.           else
  1021.             rl_point = pos;
  1022.           break;
  1023.         }
  1024.         }
  1025.       while (++pos < rl_end);
  1026.  
  1027.       if (pos >= (rl_end - 1))
  1028.         {
  1029.           ding ();
  1030.           return -1;
  1031.         }
  1032.     }
  1033.     }
  1034.   return (0);
  1035. }
  1036.  
  1037. /* Match brackets */
  1038. rl_vi_match (ignore, key)
  1039.      int ignore, key;
  1040. {
  1041.   int count = 1, brack, pos;
  1042.  
  1043.   pos = rl_point;
  1044.   if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
  1045.     {
  1046.       while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
  1047.          rl_point < rl_end - 1)
  1048.     rl_forward (1);
  1049.  
  1050.       if (brack <= 0)
  1051.     {
  1052.       rl_point = pos;
  1053.       ding ();
  1054.       return -1;
  1055.     }
  1056.     }
  1057.  
  1058.   pos = rl_point;
  1059.  
  1060.   if (brack < 0)
  1061.     {
  1062.       while (count)
  1063.     {
  1064.       if (--pos >= 0)
  1065.         {
  1066.           int b = rl_vi_bracktype (rl_line_buffer[pos]);
  1067.           if (b == -brack)
  1068.         count--;
  1069.           else if (b == brack)
  1070.         count++;
  1071.         }
  1072.       else
  1073.         {
  1074.           ding ();
  1075.           return -1;
  1076.         }
  1077.     }
  1078.     }
  1079.   else
  1080.     {            /* brack > 0 */
  1081.       while (count)
  1082.     {
  1083.       if (++pos < rl_end)
  1084.         {
  1085.           int b = rl_vi_bracktype (rl_line_buffer[pos]);
  1086.           if (b == -brack)
  1087.         count--;
  1088.           else if (b == brack)
  1089.         count++;
  1090.         }
  1091.       else
  1092.         {
  1093.           ding ();
  1094.           return -1;
  1095.         }
  1096.     }
  1097.     }
  1098.   rl_point = pos;
  1099.   return (0);
  1100. }
  1101.  
  1102. int
  1103. rl_vi_bracktype (c)
  1104.      int c;
  1105. {
  1106.   switch (c)
  1107.     {
  1108.     case '(': return  1;
  1109.     case ')': return -1;
  1110.     case '[': return  2;
  1111.     case ']': return -2;
  1112.     case '{': return  3;
  1113.     case '}': return -3;
  1114.     default:  return  0;
  1115.     }
  1116. }
  1117.  
  1118. rl_vi_change_char (count, key)
  1119.      int count, key;
  1120. {
  1121.   int c;
  1122.  
  1123.   if (vi_redoing)
  1124.     c = _rl_vi_last_replacement;
  1125.   else
  1126.     _rl_vi_last_replacement = c = rl_getc (rl_instream);
  1127.  
  1128.   if (c == '\033' || c == CTRL ('C'))
  1129.     return -1;
  1130.  
  1131.   while (count-- && rl_point < rl_end)
  1132.     {
  1133.       rl_begin_undo_group ();
  1134.  
  1135.       rl_delete (1, c);
  1136.       rl_insert (1, c);
  1137.       if (count == 0)
  1138.     rl_backward (1);
  1139.  
  1140.       rl_end_undo_group ();
  1141.     }
  1142.   return (0);
  1143. }
  1144.  
  1145. rl_vi_subst (count, key)
  1146.      int count, key;
  1147. {
  1148.   rl_begin_undo_group ();
  1149.  
  1150.   if (uppercase_p (key))
  1151.     {
  1152.       rl_beg_of_line ();
  1153.       rl_kill_line (1);
  1154.     }
  1155.   else
  1156.     rl_delete (count, key);
  1157.  
  1158.   rl_end_undo_group ();
  1159.  
  1160.   _rl_vi_set_last (key, count, rl_arg_sign);
  1161.  
  1162.   rl_begin_undo_group ();
  1163.   _rl_vi_doing_insert = 1;
  1164.   rl_vi_insertion_mode ();
  1165.  
  1166.   return (0);
  1167. }
  1168.  
  1169. rl_vi_overstrike (count, key)
  1170.      int count, key;
  1171. {
  1172.   int i;
  1173.  
  1174.   if (_rl_vi_doing_insert == 0)
  1175.     {
  1176.       _rl_vi_doing_insert = 1;
  1177.       rl_begin_undo_group ();
  1178.     }
  1179.  
  1180.   for (i = 0; i < count; i++)
  1181.     {
  1182.       vi_replace_count++;
  1183.       rl_begin_undo_group ();
  1184.  
  1185.       if (rl_point < rl_end)
  1186.     {
  1187.       rl_delete (1, key);
  1188.       rl_insert (1, key);
  1189.     }
  1190.       else
  1191.     rl_insert (1, key);
  1192.  
  1193.       rl_end_undo_group ();
  1194.     }
  1195.   return (0);
  1196. }
  1197.  
  1198. rl_vi_overstrike_delete (count)
  1199.      int count;
  1200. {
  1201.   int i, s;
  1202.  
  1203.   for (i = 0; i < count; i++)
  1204.     {
  1205.       if (vi_replace_count == 0)
  1206.     {
  1207.       ding ();
  1208.       break;
  1209.     }
  1210.       s = rl_point;
  1211.  
  1212.       if (rl_do_undo ())
  1213.     vi_replace_count--;
  1214.  
  1215.       if (rl_point == s)
  1216.     rl_backward (1);
  1217.     }
  1218.  
  1219.   if (vi_replace_count == 0 && _rl_vi_doing_insert)
  1220.     {
  1221.       rl_end_undo_group ();
  1222.       rl_do_undo ();
  1223.       _rl_vi_doing_insert = 0;
  1224.     }
  1225.   return (0);
  1226. }
  1227.  
  1228. rl_vi_replace (count, key)
  1229.      int count, key;
  1230. {
  1231.   int i;
  1232.  
  1233.   vi_replace_count = 0;
  1234.  
  1235.   if (!vi_replace_map)
  1236.     {
  1237.       vi_replace_map = rl_make_bare_keymap ();
  1238.  
  1239.       for (i = ' '; i < 127; i++)
  1240.     vi_replace_map[i].function = rl_vi_overstrike;
  1241.  
  1242.       vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
  1243.       vi_replace_map[ESC].function = rl_vi_movement_mode;
  1244.       vi_replace_map[RETURN].function = rl_newline;
  1245.       vi_replace_map[NEWLINE].function = rl_newline;
  1246.  
  1247.       /* If the normal vi insertion keymap has ^H bound to erase, do the
  1248.          same here.  Probably should remove the assignment to RUBOUT up
  1249.          there, but I don't think it will make a difference in real life. */
  1250.       if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC &&
  1251.       vi_insertion_keymap[CTRL ('H')].function == rl_rubout)
  1252.     vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete;
  1253.  
  1254.     }
  1255.   _rl_keymap = vi_replace_map;
  1256.   return (0);
  1257. }
  1258.  
  1259. #if 0
  1260. /* Try to complete the word we are standing on or the word that ends with
  1261.    the previous character.  A space matches everything.  Word delimiters are
  1262.    space and ;. */
  1263. rl_vi_possible_completions()
  1264. {
  1265.   int save_pos = rl_point;
  1266.  
  1267.   if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';')
  1268.     {
  1269.       while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' &&
  1270.          rl_line_buffer[rl_point] != ';')
  1271.     rl_point++;
  1272.     }
  1273.   else if (rl_line_buffer[rl_point - 1] == ';')
  1274.     {
  1275.       ding ();
  1276.       return (0);
  1277.     }
  1278.  
  1279.   rl_possible_completions ();
  1280.   rl_point = save_pos;
  1281.  
  1282.   return (0);
  1283. }
  1284. #endif
  1285.  
  1286. #if defined (STATIC_MALLOC)
  1287.  
  1288. /* **************************************************************** */
  1289. /*                                    */
  1290. /*            xmalloc and xrealloc ()                     */
  1291. /*                                    */
  1292. /* **************************************************************** */
  1293.  
  1294. static void memory_error_and_abort ();
  1295.  
  1296. static char *
  1297. xmalloc (bytes)
  1298.      int bytes;
  1299. {
  1300.   char *temp = (char *)malloc (bytes);
  1301.  
  1302.   if (!temp)
  1303.     memory_error_and_abort ();
  1304.   return (temp);
  1305. }
  1306.  
  1307. static char *
  1308. xrealloc (pointer, bytes)
  1309.      char *pointer;
  1310.      int bytes;
  1311. {
  1312.   char *temp;
  1313.  
  1314.   if (!pointer)
  1315.     temp = (char *)xmalloc (bytes);
  1316.   else
  1317.     temp = (char *)realloc (pointer, bytes);
  1318.  
  1319.   if (!temp)
  1320.     memory_error_and_abort ();
  1321.  
  1322.   return (temp);
  1323. }
  1324.  
  1325. static void
  1326. memory_error_and_abort ()
  1327. {
  1328.   fprintf (stderr, "readline: Out of virtual memory!\n");
  1329.   abort ();
  1330. }
  1331. #endif /* STATIC_MALLOC */
  1332.  
  1333. #endif /* VI_MODE */
  1334.